package com.demoworld.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by francis on 2017/6/25.
 */
public enum CustomMapperFactory {
    User {
        private SqlSessionFactory sqlSessionFactory;

        @Override
        public <T> T createMapper(Class<? extends IMapper> clazz) {
            return createMapper(clazz, this);
        }

        @Override
        protected void createSqlSessionFactory() {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, this.name());
        }

        @Override
        public SqlSessionFactory getSqlSessionFactory() {
            return sqlSessionFactory;
        }
    },
    Bank {
        private SqlSessionFactory sqlSessionFactory;

        @Override
        public <T> T createMapper(Class<? extends IMapper> clazz) {
            return createMapper(clazz, this);
        }

        @Override
        protected void createSqlSessionFactory() {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, this.name());
        }

        @Override
        public SqlSessionFactory getSqlSessionFactory() {
            return sqlSessionFactory;
        }
    };

    public abstract <T> T createMapper(Class<? extends IMapper> clazz);

    protected abstract void createSqlSessionFactory();

    public abstract SqlSessionFactory getSqlSessionFactory();

    private static InputStream inputStream = null;

    static {
        try {
            inputStream = Resources.getResourceAsStream("mybatis-conf.xml");
            User.createSqlSessionFactory();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }

        try {
            inputStream = Resources.getResourceAsStream("mybatis-conf.xml");
            Bank.createSqlSessionFactory();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }

    @SuppressWarnings("unchecked")
    static <T> T createMapper(Class<? extends IMapper> clazz, CustomMapperFactory mapperFactory) {
        SqlSession sqlSession = mapperFactory.getSqlSessionFactory().openSession();
        IMapper mapper = sqlSession.getMapper(clazz);
        return (T) MapperProxy.bind(mapper, sqlSession);
    }

    private static class MapperProxy implements InvocationHandler {
        private IMapper mapper;
        private SqlSession sqlSession;

        private MapperProxy(IMapper mapper, SqlSession sqlSession) {
            this.mapper = mapper;
            this.sqlSession = sqlSession;
        }

        private static IMapper bind(IMapper mapper, SqlSession sqlSession) {
            return (IMapper) Proxy.newProxyInstance(mapper.getClass().getClassLoader(),
                    mapper.getClass().getInterfaces(), new MapperProxy(mapper, sqlSession));
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Object object = null;
            try {
                object = method.invoke(mapper, args);
                sqlSession.commit();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                sqlSession.close();
            }
            return object;
        }
    }
}
